package com.facefun.service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.facefun.Enum.StateCode;
import com.facefun.co.ChaptorCo;
import com.facefun.dao.CartoonMapper;
import com.facefun.dao.ChaptorMapper;
import com.facefun.dao.PicMapper;
import com.facefun.domain.Chaptor;
import com.facefun.domain.ChaptorExample;
import com.facefun.domain.ChaptorExample.Criteria;
import com.facefun.domain.Pic;
import com.facefun.domain.PicExample;
import com.facefun.service.ChaptorService;
import com.facefun.service.IService;
import com.facefun.util.FileUploadUtil;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class ChaptorServiceImpl extends BaseService<Chaptor> implements ChaptorService{

	
	@Autowired
	public CartoonMapper cartoonMapper;
	
	@Autowired
	public PicMapper picMapper;
	
	@Override
	public ChaptorCo getChaptorCoByKey(Object ctId, Object cpId) {
		ChaptorCo cp= ((ChaptorMapper) mapper).getChapterCoByKey(ctId, cpId);
		return cp;
	}

	@Override
	public Map<String, Object> getChaptorList(Integer id,Integer offset, Integer limit) {
		ChaptorExample example =new ChaptorExample();
		Criteria c= example.createCriteria();
		if(id!=null){
			c.andCtIdEqualTo(id);
		}
		Page<Chaptor> p=PageHelper.offsetPage(offset, limit).doSelectPage(new ISelect() {
			
			@Override
			public void doSelect() {
				mapper.selectByExample(example);
				
			}
		});
		
		Long recordsTotal=p.getTotal();
		List<Chaptor> data=p.getResult();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("recordsTotal", recordsTotal);
		map.put("recordsFiltered", recordsTotal);
		map.put("data", data);
		return map;
	}

	@Override
	@Transactional
	public int addChaptor(Chaptor chaptor, MultipartFile[] files,HttpServletRequest request) {

		
		save(chaptor);
		cartoonMapper.updateChaptorNum(chaptor.getCtId(), "+1");
		
		List<Pic> pics=new ArrayList<Pic>();
		if(files!=null && !files[0].isEmpty() && files.length!=0){
			for (int i = 0; i < files.length; i++) {
				String path=FileUploadUtil.upload(files[i], request);
				Pic pic=new Pic(chaptor.getId(),path);
				pics.add(pic);
			}
			picMapper.insertList(pics);
		}
		
		return 1;
	}
	
	@Override
	@Transactional
	public int updateChaptor(Chaptor chaptor, MultipartFile[] files,HttpServletRequest request) {
	
		mapper.updateByPrimaryKeySelective(chaptor);
		
		List<Pic> pics=new ArrayList<Pic>();
		if(files!=null && !files[0].isEmpty() && files.length!=0){
			for (int i = 0; i < files.length; i++) {
				String path=FileUploadUtil.upload(files[i], request);
				Pic pic=new Pic(chaptor.getId(),path);
				pics.add(pic);
			}
			picMapper.insertList(pics);
		}
		
		return 1;
	}

	@Override
	@Transactional
	public StateCode removeChaptor(Chaptor chaptor, HttpServletRequest request) {
		
		cartoonMapper.updateChaptorNum(chaptor.getCtId(), "-1");
		
		PicExample example=new PicExample();
		example.createCriteria().andCpIdEqualTo(chaptor.getId());
		List<Pic> pics=picMapper.selectByExample(example);
		if(pics!=null && pics.size()!=0){
			for (Pic pic : pics) {
				FileUploadUtil.uploadDelete(pic.getPgImg(), request);
			}
		}
		
		picMapper.deleteByExample(example);
		if(deleteByKey(chaptor.getId())!=0){
			return StateCode.SUCCESS;
		}
			return StateCode.NOTEXISIT;

	}

	
	
}
